﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Reflection;
using System.Globalization;

namespace QLXMDataAccessLayer.Utils
{
    public class Utilities
    {        

        public static String GetNow()
        {
            String ngaythang = "Ngày " + DateTime.Now.Day + ", Tháng " + DateTime.Now.Month + ", Năm " + DateTime.Now.Year;
            return ngaythang;
        }

        public static String GetPreviousDate(String date)
        {
            DateTime MyDateTime = DateTime.ParseExact(date, "yyyy-MM-dd", null);
            DateTime NgayTruoc = MyDateTime.AddDays(-1);
            return NgayTruoc.ToString("yyyy-MM-dd");

        }

        /// <summary>
        /// Read csv file
        /// </summary>
        /// <param name="fStream"></param>
        /// <param name="strSplit"></param>
        /// <returns></returns>
        //public static DataTable ReadCSVFile(Stream fStream, char strSplit)
        //{
        //    DataTable dtResult = new DataTable();
        //    try
        //    {
        //        StreamReader re = new StreamReader(fStream, Encoding.UTF8);
        //        string input = string.Empty;
        //        int line = 0;
        //        while ((input = re.ReadLine()) != null)
        //        {
        //            //Create header of table
        //            if (line == 0)
        //            {
        //                string[] strHeader = input.Split(strSplit);
        //                for (int i = 0; i < strHeader.Length; i++)
        //                {
        //                    DataColumn column = new DataColumn(strHeader[i], Type.GetType("System.String"));
        //                    dtResult.Columns.Add(column);
        //                }
        //            }
        //            else
        //            {
        //                string[] data = input.Split(strSplit);
        //                DataRow row = dtResult.NewRow();
        //                for (int i = 0; i < data.Length; i++)
        //                {
        //                    row[i] = data[i];

        //                }
        //                dtResult.Rows.Add(row);
        //            }
        //            line++;
        //        }
        //        re.Close();
        //    }//End try
        //    catch (Exception ex)
        //    {
        //        throw new Exception(ex.Message);
        //    }
        //    finally
        //    {
        //        fStream.Close();
        //        fStream.Dispose();
        //    }
        //    return dtResult;
        //}

        /// <summary>
        /// Read excel file
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        //public static DataTable ReadFileExcel(string fileName)
        //{
        //    DataTable dtResult = new DataTable();
        //    try
        //    {
        //        Excel.Application appExl;
        //        Excel.Workbook workbook;
        //        Excel.Worksheet NwSheet;
        //        Excel.Range ShtRange;
        //        appExl = new Excel.ApplicationClass();
        //        //Opening Excel file(myData.xlsx)
        //        workbook = appExl.Workbooks.Open(fileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
        //        NwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
        //        int Cnum = 0;
        //        int Rnum = 0;

        //        ShtRange = NwSheet.UsedRange; //gives the used cells in sheet
        //        //Reading Excel file.
        //        for (Rnum = 1; Rnum <= ShtRange.Rows.Count; Rnum++)
        //        {
        //            //Create column for data table
        //            if (Rnum == 1)
        //            {
        //                //Reading Each Column value From sheet to datatable Colunms                  
        //                for (Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
        //                {
        //                    DataColumn column = new DataColumn((ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString(), Type.GetType("System.String"));
        //                    dtResult.Columns.Add(column);
        //                }
        //            }
        //            else//Create row for data table
        //            {
        //                DataRow dr = dtResult.NewRow();
        //                //Reading Each Column value From sheet to datatable Colunms                  
        //                for (Cnum = 1; Cnum <= ShtRange.Columns.Count; Cnum++)
        //                {
        //                    dr[Cnum - 1] = (ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
        //                }
        //                dtResult.Rows.Add(dr); // adding Row into DataTable
        //            }
        //        }
        //        workbook.Close(true, Missing.Value, Missing.Value);
        //        appExl.Quit();
        //    }//end try
        //    catch (Exception ex)
        //    {
        //        throw new Exception(ex.Message);
        //    }//end catch
        //    return dtResult;
        //}

        public static int ToInt(object number)
        {
            try
            {
                return Convert.ToInt32(number);
            }
            catch
            {
                return 0;
            }
        }
        public static double ToDouble(object number)
        {
            try
            {
                double rs = Convert.ToDouble(number);
                return double.IsNaN(rs) ? 0 : rs;
            }
            catch
            {
                return 0;
            }
        }
    }
}
